package com.yiren.dbaa.module.device.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yiren.dbaa.R;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.constant.StringConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.help_center.bean.DeviceUpdateBean;
import com.yiren.dbaa.view.dialog.CustomDialog;

import java.io.File;
import java.text.DecimalFormat;

/**
 * Created by WANGRONGXIANG
 * on 2022/6/16
 * Describe
 */
public class SoftwareUpdateDialog {

    private Activity activity;
    private AlertDialog dialog;
    private TextView tvDownloadRate;
    private ProgressBar pbDownloadProgress;
    private int total;
    private int sumProgressBarPosition = 0; //进度条累计进度
    private DecimalFormat df = new DecimalFormat("######0.00"); //百分比格式化工具

    public SoftwareUpdateDialog(Activity activity) {
        this.activity = activity;
    }

    public void show(DeviceUpdateBean apkUpdateBean, CommonCallBack callBack) {
        StringBuffer sb = new StringBuffer();
        sb.append("检测到新版本:");
        sb.append(apkUpdateBean.getVersionname());
        sb.append(", 是否更新?\n");
        String updateContext = "";
        for (int i = 0; i < apkUpdateBean.getContexts().size(); i++) {
            updateContext += apkUpdateBean.getContexts().get(i) + "\n";
        }
        sb.append(updateContext);
        CustomDialog.Builder builder = new CustomDialog.Builder(activity);
        builder.setTitle("系统更新");
        builder.setMessage(sb.toString());
        builder.setUpApk(true);
        builder.setPositiveButton("更新", (dialog, which) -> {
            dialog.dismiss();
            callBack.complete();
        });
        builder.setNegativeButton("暂不更新", (dialog, which) -> {
            dialog.dismiss();
        });
        builder.create().show();
    }

    public void showUpdateProgressDialog(int total) {
        this.total = total;
        dialog = new AlertDialog.Builder(activity).create();
        dialog.setCancelable(false);
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_download_status);
        TextView tvDownLoadTitle = (TextView) window.findViewById(R.id.tv_download_title);
        TextView tvDownloadName = (TextView) window.findViewById(R.id.tv_download_name);
        TextView tvDownloadSize = (TextView) window.findViewById(R.id.tv_download_size);
        tvDownloadRate = (TextView) window.findViewById(R.id.tv_download_rate);
        pbDownloadProgress = (ProgressBar) window.findViewById(R.id.pb_download_progress);
        TextView btnCancel = (TextView) window.findViewById(R.id.btn_cancel);
        TextView btnHide = (TextView) window.findViewById(R.id.btn_hide);
        //给弹出框的控件赋值，因为要用到oos的下载信息，所以和实例化控件分开写
        tvDownloadName.setText("名称：" + StringConstant.APK_NAME);
        Double download_size = (double) total / (1024 * 1024);
        tvDownloadSize.setText("大小：" + df.format(download_size) + "MB");
        tvDownloadRate.setText("0%");
        //设置进度条的最大值
        pbDownloadProgress.setMax(total);
        btnCancel.setOnClickListener(v -> {
            sumProgressBarPosition = 0;
            File file = new File(AddressConstant.App_Main_Directory() + "dbaf.apk");
            if (file.exists()) file.delete();
            dialog.dismiss();
        });
        btnHide.setOnClickListener(v -> dialog.dismiss());
    }

    public void refreshProgress(int len) {
        // 当前进度
        sumProgressBarPosition += len;
        // 设置当前进度条的当前进度
        pbDownloadProgress.setProgress(sumProgressBarPosition);
        tvDownloadRate.setText(df.format(((double) sumProgressBarPosition / total) * 100) + "%");
    }

    /**
     * 关闭对话框
     */
    public void close() {
        if (null != dialog) dialog.dismiss();
    }
}
